package com.hd.manager.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author: hzp
 * @date: 2022/5/19
 * @description: 解决 定时任务单线程卡死后,后续所有定时任务都不动
 */
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    @Bean
    public Executor taskExecutor() {
        //指定定时任务线程数量，可根据需求自行调节
        return Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    }


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }
}
